package cmd;

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

//import org.apache.log4j.Logger;

//import com.asb.minimediation.util.INMSLog;

/** 
* 控制台处理工具箱 
*/ 
public final class CmdToolkit { 
		//static Logger adapterlog = INMSLog.getInstance().getAdapterLog();

        private CmdToolkit() { 
        } 

        /** 
         * 读取控制命令的输出结果 
         * 
         * @param cmd                命令 
         * @param isPrettify 返回的结果是否进行美化（换行），美化意味着换行，默认不进行美化,当此参数为null时也不美化， 
         * @return 控制命令的输出结果 
         * @throws IOException 
         */ 
        public static String readConsole(String cmd, Boolean isPrettify) throws IOException { 
                StringBuffer cmdout = new StringBuffer(); 
               // adapterlog.info("执行命令：" + cmd); 
                Process process = Runtime.getRuntime().exec(cmd);     //执行一个系统命令 
                if (!"telnet 137.64.5.26 6000".equals(cmd)) {
                	InputStream fis = process.getInputStream(); 
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis)); 
                    String line = null; 
                    if (isPrettify == null || isPrettify) { 
                            while ((line = br.readLine()) != null) { 
                                    cmdout.append(line); 
                            } 
                    } else { 
                            while ((line = br.readLine()) != null) { 
                                    cmdout.append(line).append(System.getProperty("line.separator")); 
                            } 
                    } 
                    //adapterlog.info("执行系统命令后的结果为：\n" + cmdout.toString()); 
                }
                return cmdout.toString().trim(); 
        } 
        
        public static void main(String[] args) {
        	try {
        		//adapterlog.info(CmdToolkit.readConsole("telnet 137.64.5.26 6000", null));
				Thread.sleep(1000);
				//adapterlog.info(CmdToolkit.readConsole("lgi:op=\"zhgjxt\",pwd=\"SXdx1234\",ip=\"137.64.5.26\",pid=12345;", null));
			} catch (Exception e) {
				//adapterlog.info("error：" + e.getMessage()); 
				e.printStackTrace();
			}
		}
}